package com.iwhalecloud.bss.kite.cucc.order.service;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.KiteCustomerOrderRel;
import com.iwhalecloud.bss.kite.dataservice.entity.KiteShoppingCartGroupInst;
import com.iwhalecloud.bss.kite.dataservice.mapper.order.KiteCustomerOrderRelMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.shoppingcart.ShoppingCartGroupInstMapper;
import com.iwhalecloud.bss.kite.order.client.api.operate.IKiteCustomerOrderRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class KiteCustomerOrderRelService implements IKiteCustomerOrderRelService {

    @Autowired
    KiteCustomerOrderRelMapper kiteCustomerOrderRelMapper;

    @Autowired
    ShoppingCartGroupInstMapper shoppingCartGroupInstMapper;

    @Override
    public int insertKiteCustomerOrderRel(KiteCustomerOrderRel kiteCustomerOrderRel) {
        return kiteCustomerOrderRelMapper.insert(kiteCustomerOrderRel);
    }

    @Override
    public KiteCustomerOrderRel getKiteCustomerOrderRelByRelaCustOrderId(String relaCustOrderId, String custId) {
        Example example = new Example(KiteCustomerOrderRel.class);
        example.createCriteria().andEqualTo("relatedCustOrderId", relaCustOrderId).andEqualTo("custId", custId);
        List<KiteCustomerOrderRel> kiteCustomerOrderRels = kiteCustomerOrderRelMapper.selectByExample(example);
        if (KiteListUtils.isNotEmpty(kiteCustomerOrderRels)) {
            return kiteCustomerOrderRels.get(0);
        }
        else {
            return null;
        }
    }

    @Override
    public List<KiteCustomerOrderRel> listKiteCustomerOrderRelByCustOrderId(String custOrderId, String custId) {
        Example example = new Example(KiteCustomerOrderRel.class);
        example.createCriteria().andEqualTo("custOrderId", custOrderId).andEqualTo("custId", custId);
        List<KiteCustomerOrderRel> kiteCustomerOrderRels = kiteCustomerOrderRelMapper.selectByExample(example);
        return kiteCustomerOrderRels;
    }

    @Override
    public List<String> listAllGroupIdByCustOrderId(String custOrderId, String custId) {
        List<String> groupIds = Lists.newArrayList();
        groupIds.stream().distinct().collect(Collectors.toList());
        // 根据场景客户订单ID获取场景实例ID
        List<KiteShoppingCartGroupInst> groupInsts = shoppingCartGroupInstMapper.getGroupInstOrHisByCustOrderId(custId,
            custOrderId);
        if (KiteListUtils.isNotEmpty(groupInsts)) {
            // 获取自身场景实例ID
            groupIds.addAll(groupInsts.stream().map(KiteShoppingCartGroupInst::getGroupId).collect(Collectors.toList()));
        }
        groupIds.addAll(listRelaGroupIdByCustOrderId(custOrderId, custId));

        return groupIds.stream().distinct().collect(Collectors.toList());
    }


    @Override
    public List<String> listRelaGroupIdByCustOrderId(String custOrderId, String custId) {
        List<String> groupIds = kiteCustomerOrderRelMapper.listRelaGroupIdByCustOrderId(custOrderId, custId);
        if (KiteListUtils.isEmpty(groupIds)) {
            return Lists.newArrayList();
        }
        return groupIds.stream().distinct().collect(Collectors.toList());
    }

    /**
     * Description: <br>
     * 场景合并，通过关联客户订单ID找订单项ID
     *
     * @taskId <br>
     * @param objId
     * @param custOrderId
     * @param custId
     * @return <br>
     */
    @Override
    public List<String> listOrderItemIdByRelaCustOrderId(String objId, String custOrderId, String custId) {
        List<String> orderItemIds = kiteCustomerOrderRelMapper.listOrderItemIdByRelaCustOrderId(objId, custOrderId,
            custId);
        if (KiteListUtils.isEmpty(orderItemIds)) {
            return Lists.newArrayList();
        }
        return orderItemIds.stream().distinct().collect(Collectors.toList());
    }
}
